import { Toast, Dialog } from 'vant';

function getDbContext() {
    return window.sqlitePlugin.openDatabase({
        name: "ramac.db",
        location: 'default',
        androidDatabaseProvider: 'system'
    });
}
function initUserTable() {
    new Promise((resolve, reject) => {
        let db = getDbContext();
        db.transaction(function (tx) {
            tx.executeSql(`CREATE TABLE IF NOT EXISTS sysUser( ID INT PRIMARY KEY NOT NULL, account VARCHAR(255), password VARCHAR(255));`);
        }, function (error) {
            reject(JSON.stringify(error.message))
        }, function () {
            resolve('初始化：数据表数据化完成')
        })
    })
}
function getUserList() {
    return new Promise(function (resolve, reject) {
        try {
            let db = getDbContext();
            let list = [];
            const sqlString = `SELECT * FROM sysUser`;
            const insertSqlString = `INSERT INTO sysUser VALUES (account,password)`;
            db.transaction(function (tx) {
                tx.executeSql(insertSqlString, ['Mes01', "123789"]);
                tx.executeSql(sqlString, [], function (tx, rs) {
                    for (let index = 0; index < rs.rows.length; index++) {
                        const element = rs.rows.item(index);
                        list.push(element)
                    }
                    resolve(list);
                }, function (tx, error) {
                    reject(error.message);
                });
            })
        } catch (error) {
            reject(error);
        }
    })
}

export default {
    initUserTable,
    getUserList
}
